package com.jmz.apt.api.controller;

import com.jmz.apt.api.Helper.ResponseHelper;
import com.jmz.apt.api.enums.ResponseCode;
import com.jmz.apt.api.exception.BusinessException;
import com.jmz.apt.api.message.request.RequestMessage;
import com.jmz.apt.api.message.response.ResponseMessage;
import com.jmz.apt.api.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletResponse;
import java.util.List;


/**
 * Created by fanglexue on 2019/10/23.
 */
@RestController
@RequestMapping("login")

public class LoginController {

    @Autowired
    LoginService loginService;

    /**
     *  Login action
     * @param requestMessage
     * @return
     */
    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    public ResponseMessage doLogin(@RequestBody RequestMessage requestMessage, ServletResponse response) {
         ResponseHelper responseHelper = new ResponseHelper();
         try{
             ResponseMessage responseMessage = loginService.doLogin(requestMessage,response);
             responseHelper.setResponseBCode(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getDesc());
         }catch (BusinessException be){
            responseHelper.setResponseBCode(be.getCode(),be.getMessage());
         }

        ResponseMessage responseResult = responseHelper.getResponseMessageTemplate().getResponseMessage();
        return  responseResult;
    }


    /**
    @RequestMapping(value = "/LogOut", method = RequestMethod.POST)
    public ResponseMessage doLogOut(@RequestBody RequestMessage requestMessage) {
        RequestMessageTemplate req = new RequestMessageTemplate(requestMessage);
        AdminTokenDto adminTokenDto = SecurityContextHolder.getAgent();
        ResponseMessageTemplate responseMessageTemplate = req.getResponseMessageTemplete();
        responseMessageTemplate.setBussResponse(ResponseCode., ResponseCode.LOGIN_FAILURE.getDesc());
        if(null == adminTokenDto){
            return responseMessageTemplate.getResponseMessage();
        }
        String token = TokenUtil.conferToken(adminTokenDto, AptConstant.TOKEN_EXPIRE);
        redisTemplate.opsForHash().delete(AptConstant.Redis.LOGIN_AGENTS, AptConstant.Redis.AGENT_TOKEN + token);
        return responseMessageTemplate.getResponseMessage();
    }
    **/


}
